package martian.t1me
{
	import com.greensock.TweenMax;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import martian.t1me.core.Sequencable;
	import martian.t1me.core.Sequence;
	
	public class Tween extends EventDispatcher implements Sequencable
	{
		private var s:int = Sequence.STOPPED;
			public function get status():int { return s; }
			
		private var t:TweenMax, o:TweenMax;
		
		
		private var target:Object;
		private var duration:Number;
		private var params:Object;
		
		public function Tween(target:Object, duration:Number, params:Object, stagger:Number = 0)
		{
			this.target = target;
			this.duration = duration;
			this.params = params;
				params.paused = true;
				params.overwrite = 1;
			
			t = new TweenMax(target, duration, params);
			o = new TweenMax(this, duration - stagger, { paused:true, onComplete:stop });
		}
		
		public function start():void
		{
			if (s == Sequence.STOPPED)
			{
				dispatchEvent(new Event(Sequence.START));
				
				t.play();
				o.play();
				
				s = Sequence.RUNNING;
			}
		}
		
		private function stop():void
		{
			if (s == Sequence.RUNNING)
			{
				dispatchEvent(new Event(Sequence.STOP));
				s = Sequence.STOPPED;
			}
		}
		
		public function pause():void
		{
			if (s == Sequence.RUNNING)
			{
				t.pause();
				o.pause();
				
				s = Sequence.PAUSED;
			}
		}
		
		public function resume():void
		{
			if (s == Sequence.PAUSED)
			{
				t.resume();
				o.resume();
				
				s = Sequence.RUNNING;
			}
		}
	}
}